package org.chris.quick.b.activities;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.MenuRes;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.zhy.autolayout.AutoLayoutActivity;

import org.chris.quick.R;
import org.chris.quick.b.application.ExitApplication;
import org.chris.quick.tools.CommonUtils;
import org.chris.quick.tools.DevicesUtils;


/**
 * 对外开放的类，请继承该类
 * 使用该类须隐藏title，主题城需使用兼容的风格，详情请查看Demo的mainifests
 * Created by chris on 2016/6/8.
 *
 * @author chris Zou
 * @date 2016/6/8.
 */
public abstract class ThemeActivity extends AppCompatActivity {
    public static final String TITLE = "title";
    public static final String ID = "id";
    public static final String DATA = "data";
    public static final String TYPE = "type";

    protected View appBaseLayoutContainer;/*主布局*/
    protected Toolbar appBaseToolbar;/*标题栏*/
    protected FrameLayout appContentContainer;/*根布局，内容*/

    private Toolbar.OnMenuItemClickListener onMenuItemClickListener;
    private int resMenu = -1;
    private boolean isDefaultToolbar = false;
    protected boolean isFirst = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExitApplication.getInstance().addActivity(this);
        initView();
        init();
        isFirst = false;
    }

    private void initView() {
        if (isUsingBaseLayout()) {
            setContentView(R.layout.app_content);
            appContentContainer = (FrameLayout) findViewById(R.id.appContent);
            appBaseLayoutContainer = findViewById(R.id.parentGroupBorder);
            LayoutInflater.from(this).inflate(onResultLayoutResId(), appContentContainer);
        } else {
            appBaseLayoutContainer = LayoutInflater.from(this).inflate(onResultLayoutResId(), null);
            setContentView(appBaseLayoutContainer);
        }
        if (onResultToolbar() != null) {
            //替换Action
            appBaseToolbar = onResultToolbar();
            if (!isDefaultToolbar && isUsingBaseLayout()) {//不是默认的布局并且引用父布局
                ViewGroup viewGroup = ((ViewGroup) appBaseLayoutContainer);
                for (int i = 0; i < viewGroup.getChildCount(); i++) {
                    if (viewGroup.getChildAt(i) instanceof Toolbar) {
                        viewGroup.removeViewAt(i);
                        break;
                    }
                }
            }
            setSupportActionBar(appBaseToolbar);
            if (getIntent().hasExtra(TITLE)) {
                setTitle(getIntent().getStringExtra(TITLE));
            }
            if (!hasTitle()) {
                appBaseToolbar.setVisibility(View.GONE);
            }
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayShowHomeEnabled(true);
            }
        }
    }

    /**
     * 初始化操作
     */
    public abstract void init();

    /**
     * 是否引用基本布局
     *
     * @return
     */
    public boolean isUsingBaseLayout() {
        return true;
    }

    public void setMenu(@MenuRes int resMenu, Toolbar.OnMenuItemClickListener onMenuItemClickListener) {
        this.resMenu = resMenu;
        this.onMenuItemClickListener = onMenuItemClickListener;
    }

    public Toolbar onResultToolbar() {
        isDefaultToolbar = true;
        if (isUsingBaseLayout() && appBaseToolbar == null)
            appBaseToolbar = (Toolbar) findViewById(R.id.mainToolbar);
        return appBaseToolbar;
    }

    protected boolean hasTitle() {
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (resMenu != -1)
            getMenuInflater().inflate(resMenu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (onMenuItemClickListener != null)
            return onMenuItemClickListener.onMenuItemClick(item);
        return super.onOptionsItemSelected(item);
    }

    protected void setRightView(@LayoutRes int resId) {
        setRightView(LayoutInflater.from(this).inflate(resId, null), null);
    }

    protected void setRightView(View view) {
        setRightView(view, null);
    }

    protected void setRightView(View view, View.OnClickListener onClickListener) {
        checkNotNullToolbar();
        Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
        if (onClickListener != null)
            view.setOnClickListener(onClickListener);
        appBaseToolbar.addView(view, layoutParams);
    }

    protected void setBackInvalid(int backIcon) {
        setBackValid(backIcon, false, null);
    }

    protected void setBackValid() {
        setBackValid(-1, true, null);
    }

    protected void setBackValid(int backIcon) {
        setBackValid(backIcon, true, null);
    }

    protected void setBackValid(View.OnClickListener onClickListener) {
        setBackValid(-1, true, onClickListener);
    }

    /**
     * @param backIcon        -1:默认按钮   其他为自定义按钮
     * @param isValid         单击按钮是否有效
     * @param onClickListener
     */
    protected void setBackValid(int backIcon, boolean isValid, View.OnClickListener onClickListener) {
        checkNotNullToolbar();
        if (backIcon == -1) {
            appBaseToolbar.setNavigationIcon(R.mipmap.ic_back_arrow_white);
        } else {
            appBaseToolbar.setNavigationIcon(backIcon);
        }
        if (isValid) {
            if (onClickListener == null) {
                onClickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                };
            }
            appBaseToolbar.setNavigationOnClickListener(onClickListener);
        }
    }

    private void checkNotNullToolbar() {
        if (appBaseToolbar == null) {
            return;
        }
    }

    /**
     * 获取View
     *
     * @param resId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(@IdRes int resId) {
        return getView(resId, appBaseLayoutContainer);
    }

    /**
     * 获取View
     *
     * @param resId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(@IdRes int resId, View view) {
        return (T) view.findViewById(resId);
    }

    public void setVisibility(int visibility, int... resIds) {
        for (int resId : resIds)
            setVisibility(visibility, getView(resId));
    }

    public void setVisibility(int visibility, View... views) {
        for (View view : views)
            setVisibility(visibility, view);
    }

    public void setVisibility(int visibility, View view) {
        view.setVisibility(visibility);
    }

    /**
     * 获取常规类型数值
     *
     * @param key
     * @param defaultValue
     * @param <T>
     * @return
     */
    public <T> T getTransmitValue(String key, T defaultValue) {
        return CommonUtils.getIntentValue(getIntent(), key, defaultValue);
    }

    public Activity getActivity() {
        return this;
    }

    protected void showToast(CharSequence content) {
        showToast(content, 0, Toast.LENGTH_SHORT);
    }

    protected void showToast(CharSequence content, int gravity) {
        showToast(content, gravity, Toast.LENGTH_SHORT);
    }

    protected void showToast(CharSequence content, int gravity, int duration) {
        Toast toast = Toast.makeText(this, content, duration);
        if (gravity != 0)
            toast.setGravity(gravity, 0, 0);
        DevicesUtils.closeSoftInput(this);
        toast.show();
    }

    protected void showSnackbar(CharSequence content) {

        showSnackbar(content, null, null);
    }

    protected void showSnackbar(View view, CharSequence content) {

        showSnackbar(view, content, null, null);
    }

    protected void showSnackbar(CharSequence content, View.OnClickListener onClickListener) {

        showSnackbar(content, getString(R.string.sure), onClickListener);
    }

    protected void showSnackbar(View view, CharSequence content, View.OnClickListener onClickListener) {

        showSnackbar(view, content, getString(R.string.sure), onClickListener);
    }

    protected void showSnackbar(CharSequence content, CharSequence actionTxt, View.OnClickListener onClickListener) {
        showSnackbar(null, content, actionTxt, onClickListener);
    }

    protected void showSnackbar(View view, CharSequence content, CharSequence actionTxt, View.OnClickListener onClickListener) {
        DevicesUtils.closeSoftInput(this);
        if (view == null) {
            view = getWindow().getDecorView();
        }
        Snackbar.make(view, content, Snackbar.LENGTH_SHORT).setAction(actionTxt, onClickListener).setActionTextColor(getResources().getColor(R.color.colorBlueShallow)).show();
    }

    /**
     * 返回资源文件ID
     *
     * @return
     */
    protected abstract
    @LayoutRes
    int onResultLayoutResId();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ExitApplication.getInstance().removeActivity(this);
    }

    /**
     * 这只是一个简易的跳转方法，参数多时建议重写此方法
     *
     * @param activity
     * @param activityClass
     * @param title
     */
    public static void startAction(Activity activity, Class activityClass, String title) {
        Intent intent = new Intent(activity, activityClass);
        intent.putExtra(TITLE, title);
        activity.startActivity(intent);
    }
}
